package com.finance.system.controller;


import com.finance.base.constant.BaseConstants;
import com.finance.base.enums.StateEnum;
import com.finance.base.exception.BaseException;
import com.finance.framework.controller.BaseController;
import com.finance.framework.controller.Result;
import com.finance.framework.lang.BeanUtils;
import com.finance.framework.shiro.ShiroConfig;
import com.finance.system.bean.pojo.Menu;
import com.finance.system.bean.pojo.User;
import com.finance.system.request.MenuReq;
import com.finance.system.service.IConfigService;
import com.finance.system.service.IMenuService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 *
 */
@Controller
@RequestMapping("/system/menu")
public class MenuController extends BaseController {

    @Resource
    private IMenuService menuService;
    @Autowired
    private IConfigService configService;

    /**
     *菜单首页跳转
     * */
    @RequestMapping(value = "/index.htm",method = RequestMethod.GET)
    public String index(){
        return "system/menu/index";
    }

    /**
     * 菜单数据查询
     * */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Result list(){
        List<Menu> menus = menuService.listAll();
        return getSuccessResult(menus);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@Valid MenuReq form) throws BaseException {
        User user = getAuthUser();
        if(user == null){
            throw new BaseException(BaseConstants.ERR_UNKONW_USER);
        }
        Menu menu = new Menu();
        try {
            BeanUtils.copyProperties(menu,form);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            this.logger.error(e.getMessage());
        }
        Menu menuCheck = menuService.getBySort( menu.getSort(), menu.getPCode() );
        if( menuCheck != null && menuCheck.getId() != null ){
            throw new BaseException(BaseConstants.ERR_MENU_SORT_EXIST);
        }
        menu.setCreateUid(user.getId());
        menu.setState(StateEnum.Valid.getState());
        menuService.add(menu);
        return getSuccessResult(true);
    }


    /**
     * 菜单更新功能接口
     * */
    @RequestMapping(value ="/update",method = RequestMethod.POST)
    public Result update(@Valid MenuReq form){
        if(StringUtils.isBlank(form.getId())){
            throw new BaseException(BaseConstants.ERR_INVALID_PARAMETER);
        }
        User user=getAuthUser();
        if(user == null){
            throw new BaseException(BaseConstants.ERR_UNKONW_USER);
        }
        Menu menu = new Menu();
        try {
            BeanUtils.copyProperties(menu,form);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            this.logger.error(e.getMessage());
        }
        Menu menuCheck = menuService.getBySort( menu.getSort(), menu.getPCode() );
        if( menuCheck != null && menuCheck.getId() != null && !menuCheck.getId().equals(menu.getId()) ){
            throw new BaseException(BaseConstants.ERR_MENU_SORT_EXIST);
        }
        menu.setUpdateUid(user.getId());
        menuService.update(menu);
        return getSuccessResult(true);
    }

    /**
     * 菜单删除接口
     * @param id
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public Result del(String id) throws BaseException {
        if (StringUtils.isBlank(id)){
            throw new BaseException(BaseConstants.ERR_INVALID_PARAMETER);
        }
        User user = getAuthUser();
        if(user==null){
            throw new BaseException(BaseConstants.ERR_UNKONW_USER);
        }
        boolean ret=menuService.del(Long.parseLong(id),user.getId().toString());
        return getSuccessResult(ret);
    }
    @RequestMapping("/getTimmerBaseUrl")
    public Result getTimmerBaseUrl(){
        String value = this.configService.findConfig(BaseConstants.SYSCONFIG_KEY_TIMMER_BASE_URL);
        Map<String , String> result = new HashMap<String,String>();
        if(StringUtils.isNotBlank(value)){
            result.put("baseUrl",value);
            result.put("JSESSIONID",this.getSessionContext().getId());
            return this.getSuccessResult(result);
        }else{
            return this.getFailResult(BaseConstants.ERR_UNKOWN_CONFIG,BaseConstants.getErrorMessage(BaseConstants.ERR_UNKOWN_CONFIG));
        }
    }
}